package no.ffi.gismoidm.attrex;

import java.util.Properties;

/**
 *
 * @author anders
 */
public class StringOperand implements ScalarOperand { 
    protected String value;
    public StringOperand(String value) { this.value = value;}
    public String getStringValue(Properties attributes) { 
        return value; 
    }

    
    @Override
    public String toString() { return "\"" + value + "\""; }
   
    public boolean equals(Object o, Properties prop) {
        if (o instanceof StringOperand) {
            StringOperand ss = (StringOperand)o;
            String s2 = ss.getStringValue(prop);
            String s0 = this.getStringValue(prop);
            if (s2==null || s0==null) return false;
            return s0.equals(s2);
        } else if (o instanceof AttributeOperand) {
            AttributeOperand ss = (AttributeOperand)o;
            String s2 = ss.getStringValue(prop);
            String s0 = this.getStringValue(prop);
            if (s2==null || s0==null) return false;
            return s0.equals(s2);
        } else if (o instanceof IntegerOperand) {
            IntegerOperand io = (IntegerOperand)o;
            try {
                Integer ii = io.getNumberValue();
                String s3 = getStringValue(prop);
                if (io==null || s3==null) return false;
                Integer t = Integer.parseInt(s3.trim());
                return ii.equals(t);
            } catch (java.lang.NumberFormatException e) {
                return false; 
            }
        }
        else return false;
    }
     
    
    @Override
    public boolean greaterThan(ScalarOperand so, Properties attributes) throws AttributeEvaluationException {
        if (so instanceof StringOperand) {
            // In case "so" is attribute-dependant, let it calculate its own value
            String compare = ((StringOperand)so).getStringValue(attributes);
            String thisValue = this.getStringValue(attributes);
            if (compare==null || thisValue==null)
                throw new AttributeEvaluationException("cannot compare nonexistent values");
            return (thisValue.compareTo(compare) > 0);
        } else if (so instanceof NumberOperand) {
            Number n2 = ((NumberOperand)so).getNumberValue();
            Number n1 = this.toNumberValue(attributes);
            if (n1==null || n2==null) 
                return false; //throw new AttributeEvaluationException("cannot compare nonexistent values");
            return n1.doubleValue()>n2.doubleValue();
        } else throw new AttributeEvaluationException("Incompatible types");
    }
    
    private Number toNumberValue(Properties attrs) throws AttributeEvaluationException {
        String s0 = this.getStringValue(attrs);
        if (s0==null) 
            return null; //throw new AttributeEvaluationException("attempt to convert a nonexistent value");
        // Check syntax of number string and determine the type of return
        // value. Integer is only option for the moment
        try {
            Integer i0 = Integer.parseInt(s0.trim());
            return i0;
        } catch (NumberFormatException e) { 
            throw new AttributeEvaluationException("syntax error in number data");
        }
    }
}
